﻿/*****************************************************************
 *@Copyright all rights reserved © 2014 Yuga
 *@author YUga studio
 *Contact: zengkang007@gmail.com
 *****************************************************************/
package com.oplink.webbase.modules.invoicing.service;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.oplink.webbase.common.persistence.Page;
import com.oplink.webbase.modules.invoicing.entity.Sales;
import com.oplink.webbase.modules.invoicing.dao.SalesDao;



/**
 * 告警Service
 * @author YUga studio
 * @version 2015-01-12
 */
@Component
@Transactional
public class SalesService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(SalesService.class);
	
	@Autowired
	private SalesDao salesDao;
	
	public Sales get(Long id) {
		return salesDao.findOne(id);
	}
	
	public Page<Sales> find(Page<Sales> page, Sales sales) {
		DetachedCriteria dc = salesDao.createDetachedCriteria();
		//添加查询条件
		if (null != sales.getSaleFormId()
				&& StringUtils.isNotEmpty(sales.getSaleFormId().toString())) {
			dc.add(Restrictions.like("saleFormId", "%"+sales.getSaleFormId()+"%"));
		}
        if (StringUtils.isNotEmpty(sales.getDataTime())){
			dc.add(Restrictions.like("dataTime", "%"+sales.getDataTime()+"%"));
		}
		if (null != sales.getOperatman()
				&& StringUtils.isNotEmpty(sales.getOperatman().toString())) {
			dc.add(Restrictions.like("operatman", "%"+sales.getOperatman()+"%"));
		}
		dc.add(Restrictions.eq(Sales.DEL_FLAG, Sales.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return salesDao.find(page, dc);
	}
	
	/**
	  * @function 创建查询
	  **/
	public Page<Sales> getUserSales(Page<Sales> page, Sales sales) {
		DetachedCriteria dc = salesDao.createDetachedCriteria();
		//添加查询条件
		if (null != sales.getSaleFormId()
				&& StringUtils.isNotEmpty(sales.getSaleFormId().toString())) {
			dc.add(Restrictions.like("saleFormId", "%"+sales.getSaleFormId()+"%"));
		}
        if (StringUtils.isNotEmpty(sales.getDataTime())){
			dc.add(Restrictions.like("dataTime", "%"+sales.getDataTime()+"%"));
		}
		if (null != sales.getOperatman()
				&& StringUtils.isNotEmpty(sales.getOperatman().toString())) {
			dc.add(Restrictions.like("operatman", "%"+sales.getOperatman()+"%"));
		}
		dc.add(Restrictions.eq(Sales.DEL_FLAG, Sales.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return salesDao.find(page, dc);
	}
	
	public Sales getSales(Long id) {
		return salesDao.findOne(id);
	}

	public void saveSales(Sales entity) {
		salesDao.save(entity);
	}
	
	public void deleteSales(Long id) {
		salesDao.delete(id);
	}
	
	@Autowired
	public void setSalesDao(SalesDao salesDao) {
		this.salesDao = salesDao;
	}	
}
